﻿using AutoMapper;
using System;

namespace LINGYUN.Abp.Notifications;

public class AbpNotificationsApplicationAutoMapperProfile : Profile
{
    public AbpNotificationsApplicationAutoMapperProfile()
    {
        CreateMap<UserNotificationInfo, UserNotificationDto>()
            .ForMember(dto => dto.Id, map => map.MapFrom(src => src.NotificationId.ToString()))
            .ForMember(dto => dto.Lifetime, map => map.Ignore())
            .ForMember(dto => dto.Data, map => map.MapFrom((src, nfi) =>
            {
                if (src != null)
                {
                    var dataType = Type.GetType(src.NotificationTypeName);
                    if (dataType != null)
                    {
                        var data = Activator.CreateInstance(dataType);
                        if (data is NotificationData notificationData)
                        {
                            notificationData.ExtraProperties = src.ExtraProperties;
                            return notificationData;
                        }
                    }
                }
                return new NotificationData();
            }));
    }
}
